--光刻蚀痕
local guangkeshihen = fk.CreateSkill {
    name = "mcdh_guangkeshihen",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

guangkeshihen:addAcquireEffect(function (self, player)
    local room = player.room
    local mark = {}
    local events = room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
        if table.contains(e.data:getAllTargets(),player) then
            table.insertIfNeed(mark,e.data.card.trueName)
        end
        return false
    end, Player.HistoryGame)
    --成为过目标的牌
    room:setPlayerMark(player,"#guangkeshihen_card",mark)
    --显示能用的牌
    local canuse = {}
    for _, id in ipairs(Fk:getAllCardIds()) do
        local card = Fk:getCardById(id)
        if (card.type == Card.TypeBasic or ( card:isCommonTrick() and not card.multiple_targets )) and not card.is_derived then
            table.insertIfNeed(canuse,card.trueName)
        end
    end
    canuse = table.filter(canuse,function(name) return not table.contains(mark,name)  end)
    room:setPlayerMark(player,"@$guangkeshihen_canuse",canuse)
end)

guangkeshihen:addEffect('viewas',{
    pattern = ".",
    interaction = function(self,player)
        local names = {}
        local mark = player:getTableMark("#guangkeshihen_card")
        local mark2 = player:getTableMark("#guangkeshihen_carduse-phase")
        for _, id in ipairs(Fk:getAllCardIds()) do
            local card = Fk:getCardById(id)
            if (card.type == Card.TypeBasic or ( card:isCommonTrick() and not card.multiple_targets )) and not card.is_derived then
                local to_use = Fk:cloneCard(card.name)
                if (Fk.currentResponsePattern == nil and player:canUse(to_use) and not player:prohibitUse(to_use)) then
                    if not table.contains(mark, card.trueName) and not table.contains(mark2, card.trueName) then
                        table.insertIfNeed(names, card.name)
                    end
                end
            end
        end
        if #names == 0 then return end
        return  U.CardNameBox { choices = names}
    end,
    card_filter =function(self,player, to_select, selected)
        return #selected == 0 and not table.contains(player:getTableMark("#guangkeshihen_color-phase"),Fk:getCardById(to_select).color)
    end,
    view_as = function(self,player, cards)
        if not self.interaction.data then return end
        if #cards == 0 then return end
        local card = Fk:cloneCard(self.interaction.data)
        card:addSubcard(cards[1])
        card.skillName = self.name
        return card
    end,
    before_use = function (self, player, use)
        local room = player.room
        local mark = player:getTableMark("#guangkeshihen_color-phase")
        local mark2 = player:getTableMark("#guangkeshihen_carduse-phase")
        table.insertIfNeed(mark,use.card.color)
        table.insertIfNeed(mark2,use.card.trueName)
        room:setPlayerMark(player,"#guangkeshihen_color-phase",mark)
        room:setPlayerMark(player,"#guangkeshihen_carduse-phase",mark2)
    end,
    after_use = function (self, player, use)
        local room = player.room
    end,
    enabled_at_play = function(self, player)
        return true
    end,
    enabled_at_response = function(self, player, response)
        return  false
    end,
})

guangkeshihen:addEffect(fk.CardUseFinished,{
    can_refresh = function(self, event, target, player, data)
            return player:hasSkill(self.name,true)
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
            if table.contains(data:getAllTargets(),player) then
                local mark = player:getTableMark("#guangkeshihen_card")
                table.insertIfNeed(mark,data.card.trueName)
                room:setPlayerMark(player,"#guangkeshihen_card",mark)


                local canuse = player:getTableMark("@$guangkeshihen_canuse")
                table.removeOne(canuse,data.card.trueName)
                room:setPlayerMark(player,"@$guangkeshihen_canuse",canuse)
            end
    end
})

Fk:loadTranslationTable {
    ["mcdh_guangkeshihen"] = "刻痕",
    [":mcdh_guangkeshihen"] = "出牌阶段每种颜色限一次，你可以将一张牌当做任意基本牌或单目标普通锦囊牌使用（本局游戏含你为目标且结算完成的牌名除外，本回合你以此法使用过的牌名除外）。",
    ["@$guangkeshihen_canuse"] = "刻痕"
}

return guangkeshihen